package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.base.exception.Assert;
import com.atguigu.srb.base.exception.SrbBussinessException;
import com.atguigu.srb.base.result.R;
import com.atguigu.srb.base.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-01-04
 */
@RestController
@Slf4j
 //解决跨域问题
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级管理模块") //描述controller
public class AdminIntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;
    /*
        批量删除：  id 1 2 3 4 5
            id参数如何接受
            传参方式1：路径参数  id参数个数不固定  pass

            传参方式2：请求参数   url?param1=xx&param2=xx
                    url?id=1&id=2&id=3&id=4&id=5
                    url?id=1,2,3,4,5
                    url?username=zhanngsan&password=123456  ===>  User{ username  password}
                        pojo入参： User user
                获取参数：@RequestParam List<String> id
            传参方式3：请求体参数   请求体只有一个通常以json字符串方式提交参数
                    一个json对象：{}
                    json数组：[1,2,3,4,5]
                     必须通过@RequestBody获取参数
     */
    @ApiOperation("批量删除")
    @DeleteMapping("batchDel")
    public R batchDel(@RequestBody List<String> ids){
//        System.out.println(ids);
        integralGradeService.removeByIds(ids);
        return R.ok();
    }
    //1、查询所有的积分等级
    @ApiOperation(value = "查询所有")
    @GetMapping
    public R list(){
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("items",list);
    }
    //2、根据id查询积分等级
    @ApiOperation(value = "根据id查询")
    @GetMapping("{id}") // get +   /integralGrade/{id}
    public R getById(@ApiParam(value = "id" , required = true) @PathVariable("id")String id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        Assert.assertObjIsNull(integralGrade,ResponseEnum.ERROR);
        return R.ok().data("item" , integralGrade);
    }
    @ApiOperation(value = "更新")
    @PutMapping
    public R update(@RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.updateById(integralGrade);
        Assert.assertNotTrue(b,ResponseEnum.ERROR);
        return R.ok();
    }
    //3、删除积分等级
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("{id}") // delete +   /integralGrade/{id}
    public R deleteById(@ApiParam(value = "id" , required = true)@PathVariable("id")String id){
        boolean b = integralGradeService.removeById(id);
        Assert.assertNotTrue(b,ResponseEnum.ERROR);
        return R.ok();
    }
    @ApiOperation(value = "分页查询")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R list(@ApiParam(value = "页码")@PathVariable("pageNum")Integer pageNum,
                                    @ApiParam(value = "每页数量")@PathVariable("pageSize")Integer pageSize){
        return R.ok().data("page",integralGradeService.page(new Page<>(pageNum,pageSize)));
    }
    @ApiOperation(value = "新增")
    @PostMapping
    public R save(@RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.save(integralGrade);
        Assert.assertNotTrue(b,ResponseEnum.ERROR);
        return R.ok();
    }
}

